<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态路由匹配</title>
    <script src="https://lib.baomitu.com/vue/2.6.10/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
    <div id="app">
        <h1>Hello App</h1>
        <p>
            <router-link to="/foo/1">Go to Foo 1</router-link>
            <router-link to="/foo/2">Go to Foo 2</router-link>
            <router-link to="/bar">Go to bar</router-link>
        </p>

        <router-view></router-view>
    </div>

<script>
    const Foo = {
        template : '<div>foo-{{$route.params.id}}</div>',
        beforeRouteUpdate  (to, from, next){
            console.log('before---', to, from, next)
            next()
        },
    }
    const Bar = {template : '<div>bar</div>'}

    const routes = [
        {path : '/foo/:id', component : Foo},
        {path : '/bar', component : Bar}
    ]

    const router = new VueRouter({
        routes  //缩写，相当于 routers : routers
    });

    const app = new Vue({
        router,
        data : {
            a : 1
        },
        beforeCreate : function (){
            console.log("在实例初始化之后，数据观测(data observer) 和 event/watcher 事件配置之前被调用！");
        },
        created: function () {
            console.log('实例已经创建完成之后被调用。在这一步，实例已完成以下的配置：数据观测(data observer)，属性和方法的运算， watch/event 事件回调。然而，挂载阶段还没开始，$el 属性目前不可见。'+
                '输出数据：' + this.a)
        },
        beforeMount:function(){
            console.log("在挂载开始之前被调用：相关的 render 函数首次被调用。")
        },
        mounted : function (){
            console.log("el 被新创建的 vm.$el 替换，并挂载到实例上去之后调用该钩子。")
        },
        watch: {
            '$route' (to, from) {
                // 对路由变化作出响应...
                console.log('watch --- ', to, from)
            }
        }
    }).$mount("#app");
</script>
</body>
</html>
